package hovn.app.quickpay.widget;

import hovn.app.quickpay.R;
import hovn.app.quickpay.activity.pay.AlipayScan;
import hovn.app.quickpay.activity.pay.AlipayFukuan;
import hovn.app.quickpay.activity.pay.WechatFukuan;
import hovn.app.quickpay.activity.pay.WechatScan;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

/**
 * Created by guhaibo on 2017/10/13.
 */

public class Widget1 extends AppWidgetProvider {

	// 定义我们要发送的事件
	public static final String WIDGET_BROADCAST = "WIDGET_BROADCAST";

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		super.onDeleted(context, appWidgetIds);
	}

	@Override
	public void onEnabled(Context context) {
		super.onEnabled(context);
		Intent intent = new Intent();
		intent.setAction(Widget1.WIDGET_BROADCAST);
		context.sendBroadcast(intent);
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		Intent intent = new Intent();
		intent.setAction(Widget1.WIDGET_BROADCAST);
		context.sendBroadcast(intent);
	}

	@Override
	public void onReceive(final Context context, Intent intent) {
		if (intent.getAction().equals(WIDGET_BROADCAST)) {
			RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget1_layout);

			// rv.setTextViewText(R.id.title, "KJZF");

			Intent intentNew = new Intent(context, AlipayScan.class);
			PendingIntent pending = PendingIntent.getActivity(context, 0, intentNew, PendingIntent.FLAG_UPDATE_CURRENT);
			rv.setOnClickPendingIntent(R.id.rl_zfbskm, pending);

			intentNew = new Intent(context, AlipayFukuan.class);
			pending = PendingIntent.getActivity(context, 0, intentNew, PendingIntent.FLAG_UPDATE_CURRENT);
			rv.setOnClickPendingIntent(R.id.rl_zfbme, pending);

			intentNew = new Intent(context, WechatFukuan.class);
			pending = PendingIntent.getActivity(context, 0, intentNew, PendingIntent.FLAG_UPDATE_CURRENT);
			rv.setOnClickPendingIntent(R.id.rl_zfbfkm, pending);

			intentNew = new Intent(context, WechatScan.class);
			pending = PendingIntent.getActivity(context, 0, intentNew, PendingIntent.FLAG_UPDATE_CURRENT);
			rv.setOnClickPendingIntent(R.id.rl_zfbsys, pending);

			AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
			ComponentName componentName = new ComponentName(context, Widget1.class);
			appWidgetManager.updateAppWidget(componentName, rv);
		}
		super.onReceive(context, intent);
	}
}
